package com.example.jbmsuppliers.sinopec.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.LoginActivity;
import com.example.jbmsuppliers.sinopec.fragment.ListFragment;
import com.example.jbmsuppliers.sinopec.net.NetDeceVersion;
import com.example.jbmsuppliers.sinopec.view.DoubleButtonDialog;
import com.example.permission.myjbm.adapter.MyFragmentAdapter_gv;
import com.example.permission.myjbm.config.Config;
import com.example.permission.myjbm.utils.ImageLoaderInit;
import com.example.permission.myjbm.utils.LoginSharedPreferences;
import com.example.permission.myjbm.view.MeauseGridView;
import com.example.weather.smoothImage.ImagePagerActivity;
import com.jbm.jbmsupplier.util.ICtrl;
import com.jbm.jbmsupplier.util.InfomationEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static com.jbm.jbmsupplier.util.abstracts.SendMessageInterfase.gSocket;

/**
 * 朋友圈的适配器   与MyFragment相关联
 */

public class MyFragmentAdapter_lz extends BaseAdapter{

//    private final MyPopupWindow popup;
    private final LoginSharedPreferences sp;

    private ArrayList<InfomationEntity> arr1;
    private Context c;
    private MeauseGridView gv;
    private ArrayList<String> picArr;
    private String ty;
    private final int CANCELSOUC=0001;
    private final int DELETEFAB=0002;
    ListFragment listFragment ;
    private InfomationEntity info;
    private SimpleDateFormat df;


    public MyFragmentAdapter_lz(List<InfomationEntity> arr, Context c,String types,ListFragment fragment,SimpleDateFormat df){
        arr1= (ArrayList<InfomationEntity>) arr;
        this.listFragment=fragment;
        this.ty=types;
        this.c=c;
        picArr=new ArrayList<String>();
        sp=new LoginSharedPreferences(c);
        this.df = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
//时间为设置

    }
    public void setNotify(List<InfomationEntity> arr){
        arr1= (ArrayList<InfomationEntity>) arr;
        Log.e("main","适配器"+arr1.size());
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return arr1.size();
    }

    @Override
    public Object getItem(int position) {
        return arr1.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
         MyHolder holder=null;
                if(convertView==null){
                    convertView =View.inflate(c, R.layout.lz_adapter_item1,null);
                    holder=new MyHolder(convertView);
                    convertView.setTag(holder);
                }else{
                    holder= (MyHolder) convertView.getTag();
                }
       info= arr1.get(position);
                initView(info,position,holder);
        return convertView;
    }
//初始化数据
    private void initView(InfomationEntity infomationEntity,int position ,MyHolder holder) {
        contentDispose(infomationEntity,position,holder);
        holder. item_date.setText(infomationEntity.getDealtime().equals("0")?df.format(infomationEntity.getUpdateDate()):infomationEntity.getDealtime());
        holder.item_user_name.setText(infomationEntity.getUserinfo().getNickname());

        if (infomationEntity.getUserinfo().getLogo()!=null){
        if(infomationEntity.getUserinfo().getLogo().length()<5){
            holder.item_user_img.setImageResource(R.mipmap.pic);
        }else {
            ImageLoaderInit.getInstance().photoShow(infomationEntity.getUserinfo().getLogo(), holder.item_user_img);
        }}
        pictureBrowse(infomationEntity,holder);
        bounced(holder,infomationEntity,position);

    }

    //用来记录当前点击的位置  服务器回调用
    private int currentIndex;
    /**
     * 弹出关注和点赞
     * @param holder
     */
    private void bounced(final MyHolder holder, final InfomationEntity infomationEntity, final int possition) {

        //删除订单
        holder.item_isAttention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(sp.getLoginState()!= true){
                    c.startActivity(new Intent(c, LoginActivity.class));
                    return;
                }
                currentIndex=possition;
                if(ty.equals("搜藏")){
                    //收藏调的删除接口
                    final    DoubleButtonDialog   dialog = new DoubleButtonDialog(
                            c,"确定要删除吗？");
                    dialog.getokbtn().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            NetDeceVersion netDeceVersion=new NetDeceVersion(handler);
                            netDeceVersion.setdeleteMyll(infomationEntity.getInfoId(),gSocket.login_id,1,CANCELSOUC);
                            dialog.dismiss();
                        }
                    });
                    dialog.getnobtn().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
//                    dialog.setisVisibility();
                }else if (ty.equals("")){
                    //发布调删除接口
                    final    DoubleButtonDialog   dialog = new DoubleButtonDialog(
                            c,"确定要删除吗？");
                    dialog.getokbtn().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            NetDeceVersion netDeceVersion=new NetDeceVersion(handler);
                            netDeceVersion.setdeleteIssue(infomationEntity.getInfoId(),DELETEFAB);
                            dialog.dismiss();
                        }
                    });
                    dialog.getnobtn().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });

                }
            }
        });
    }

    //  public int cout;
    /**
     * 处理内容   收缩及显示
     *
     */
    private void contentDispose(final InfomationEntity infomationEntity, final int position, final MyHolder holder) {
        //    dian.init(holder.item_isShow, position,holder.item_isShow);
     //   holder.item_isShow.setVisibility(View.GONE);
        if(infomationEntity.getAuditinfo()==null){
            holder. item_error1.setVisibility(View.GONE);
        }else{
            holder. item_error1.setVisibility(View.VISIBLE);
            holder. item_error1.setText("错误信息:"+infomationEntity.getAuditinfo().getRefuseinfo());


        }



        holder.item_content.setText(infomationEntity.getContent());
        holder.item_isShow.setVisibility(View.GONE);
        //获取行高
        holder.item_content.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {

                holder.item_content.getViewTreeObserver().removeOnPreDrawListener(this);
                int cout = holder.item_content.getLineCount();
                infomationEntity.setMaxnum(cout);
                if (cout > 5) {
                    holder.item_isShow.setVisibility(View.VISIBLE);
                } else {
                    holder.item_isShow.setVisibility(View.GONE);
                }
                return true;
            }
        });
        if(infomationEntity.getIsShow()==0){
            holder.item_content.setMaxLines(5);
            holder.item_isShow.setText("全文");
        }else{
            holder.item_content.setMaxLines(infomationEntity.getMaxnum());
            holder.item_isShow.setText("收起");
        }

        holder.item_isShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (infomationEntity.getIsShow()==0) {
                    holder.item_content.setMaxLines(infomationEntity.getMaxnum());
                    infomationEntity.setIsShow(1);
                    holder.item_isShow.setText("收起");
                } else {
                    holder.item_content.setMaxLines(5);
                    infomationEntity.setIsShow(0);
                    holder.item_isShow.setText("全文");

                }
                holder.item_content.setText(arr1.get(position).getContent());
            }
        });

    }

    //浏览图片
    private void pictureBrowse(final InfomationEntity infomationEntity,MyHolder holder) {
        holder.item_photo_video.removeAllViews();
        gv=new MeauseGridView(c);
        gv.setAdapter(new MyFragmentAdapter_gv(infomationEntity.getAttList(),c));
        holder.item_photo_video.addView(gv);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                picArr.clear();
                for(int i=0;i<infomationEntity.getAttList().size();i++){//infomationEntity.getAttList().size()
                    picArr.add(Config.PHOTOPATH+infomationEntity.getAttList().get(i).getThumAttPath());
//                    picArr.add(infomationEntity.getAttList().get(i).getThumAttPath());
                }

                Intent intent = new Intent(c, ImagePagerActivity.class);
                        // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
                        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, picArr);
                        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                        c.startActivity(intent);
            }
        });
    }
    public class MyHolder{
        private final ImageView item_user_img,item_isAttention;
        private final TextView item_user_name,item_date;
        public final TextView item_content,item_isShow,item_error1;
        private final LinearLayout item_photo_video,big;
        public MyHolder(View convertView){
            //用户头像
            item_user_img=(ImageView)convertView.findViewById(R.id.item_user_imgi);
            //用户名称
            item_user_name=(TextView)convertView.findViewById(R.id.item_user_namei);
            //用户发送的文字
            item_content=(TextView)convertView.findViewById(R.id.item_contenti);
            //收起和显示全部
            item_isShow=(TextView)convertView.findViewById(R.id.item_isShowi);
            //是图片还是添加视频
            item_photo_video=(LinearLayout)convertView.findViewById(R.id.item_photo_videoi);
            //显示点赞和关注的数量
            //点赞
            item_isAttention=(ImageView)convertView.findViewById(R.id.item_isAttentioni);
            //最外层的
            big=(LinearLayout)convertView.findViewById(R.id.bigi);
            //时间
            item_date=(TextView)convertView.findViewById(R.id.item_datei);
            //错误信息
            item_error1=(TextView)convertView.findViewById(R.id.item_error1);


        }
    }
public interface InformUpdate{
        void MyLissueUpdate(int i);
        void MyLlactionUpdate();
    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case CANCELSOUC:{
                    ICtrl cancel=(ICtrl)msg.obj;
                    if (cancel.back){
                   //     ((MyLlaciton)c).MyLlactionUpdate();
                        arr1.remove(currentIndex);
                        MyFragmentAdapter_lz.this.setNotify(arr1);
                    }else if (cancel.equals("time out")){
                        return;
                    }else{
                        return;
                    }
                    break;
                }
                case DELETEFAB:{
                    ICtrl cancel=(ICtrl)msg.obj;
                    if (cancel.back){
                     //   listFragment.MyLissueUpdate(info.getStatus());
                        arr1.remove(currentIndex);
                        MyFragmentAdapter_lz.this.setNotify(arr1);
                    }else if (cancel.equals("time out")){
                        return;
                    }else{
                        return;
                    }

                    break;
                }

            }

            super.handleMessage(msg);
        }
    };


}
